#include <signal.h>
#include "front_thread.h"
#include "front_server.h"
#include "lf_thread.h"
#include "lf_server.h"
#include "Log.h"

using namespace std;

int main(int argc, char **argv)
{
	string path = "./logs";
	string log_level = "DEBUG";
	if (!init_logger(path, log_level)) {
		cout << "logger init failed log[@path | @level]" << endl;
		return -1;
	}

	vector<server *> servers;

	string ip = "199.155.122.11";
	string port = "45500";
	address addr;
	//front 
	front_server *front = new front_server;
	port = "45500";
	addr.set_address(ip,port);
	front->set_front_address(addr);
	port = "45510";
	addr.set_port(port);
	front->set_back_address(addr);
	//add lf_extract
	port = "45504";
	addr.set_port(port);
	front->set_lf_extract(addr);
	//add lf_merge
	port = "45505";
	addr.set_port(port);
	front->set_lf_merge(addr);
	
	front->set_thread_num(10);
	front_thread *ft = NULL;
	for(int i = 0; i < front->get_thread_pool()->get_thread_num(); i++){
		ft = new front_thread(front->get_thread_pool());
		front->get_thread_pool()->add_thread(ft);
	}
	servers.push_back(front);

	//lf_extract
	lf_extract_server *lf_extract = new lf_extract_server;
	port = "45504";
	addr.set_port(port);
	lf_extract->set_front_address(addr);
	port = "45514";
	addr.set_port(port);
	lf_extract->set_back_address(addr);

	lf_extract->set_thread_num(10);
	lf_extract_thread *lft = NULL;
	for(int i = 0; i < lf_extract->get_thread_pool()->get_thread_num(); i++){
		lft = new lf_extract_thread(lf_extract->get_thread_pool());
		lf_extract->get_thread_pool()->add_thread(lft);
	}
	servers.push_back(lf_extract);

	//lf_merge
	lf_merge_server *lf_merge = new lf_merge_server;
	port = "45505";
	addr.set_port(port);
	lf_merge->set_front_address(addr);
	port = "45515";
	addr.set_port(port);
	lf_merge->set_back_address(addr);

	//add lf_match
	port = "45506";
	addr.set_port(port);
	lf_merge->add_lf_match(addr);

	lf_merge->set_thread_num(10);
	lf_merge_thread *lf_merge_t = NULL;
	for(int i = 0; i < lf_merge->get_thread_pool()->get_thread_num(); i++){
		lf_merge_t = new lf_merge_thread(lf_merge->get_thread_pool());
		lf_merge->get_thread_pool()->add_thread(lf_merge_t);
	}
	servers.push_back(lf_merge);

	//lf_match
	lf_match_server *lf_match = new lf_match_server;
	port = "45506";
	addr.set_port(port);
	lf_match->set_front_address(addr);
	port = "45516";
	addr.set_port(port);
	lf_match->set_back_address(addr);

	lf_match->set_thread_num(10);
	lf_match_thread *gmt = NULL;
	for(int i = 0; i < lf_match->get_thread_pool()->get_thread_num(); i++){
		gmt = new lf_match_thread(lf_match->get_thread_pool());
		lf_match->get_thread_pool()->add_thread(gmt);
	}
	servers.push_back(lf_match);

	//start all server
	for (vector<server *>::iterator it = servers.begin(); it != servers.end(); it++)
		(*it)->start();

	while(1) sleep(1);

	return 0;
}


